# -*- coding: utf-8 -*-

__author__ = 'bitfeng'
# Define your db_connect here

import MySQLdb
import psycopg2
from twisted.enterprise import adbapi
from pymongo import MongoClient


class MongoDBConnect(object):

    @classmethod
    def createConn(cls, mongodb_uri):
        try:
            conn = MongoClient(
                host=mongodb_uri['host'],
                port=mongodb_uri['port'],
            )
            db = conn[mongodb_uri['database']]
            db.authenticate(name=mongodb_uri['user_name'], password=mongodb_uri['password'])
            return conn
        except:
            print 'Mongo connected Fail!'

# class PyMongoConnect(object):
#
#     def __init__(self, host, port):
#      #conn 类型<class 'pymongo.connection.Connection'>
#      try:
#          self.conn = MongoClient(host, port)
#      except  Error:
#          print 'pymongo connect to %s:%s fail' % (host, port)
#          exit(0)
#
#     def close(self):
#         self.conn.close()
#
#     def use(self, dbname):
#         # 这种[]获取方式同样适用于shell,下面的collection也一样
#         # db 类型<class 'pymongo.database.Database'>
#         self.db = self.conn[dbname]
#
#     def setCollection(self, collection):
#         if not self.db:
#             print 'don\'t assign database'
#             exit(0)
#         else:
#             self.coll = self.db[collection]
#
#     def find(self, query={}):
#         # 注意这里query是dict类型
#         if type(query) is not dict:
#             print 'the type of query isn\'t dict'
#             exit(0)
#         try:
#             # result类型<class 'pymongo.cursor.Cursor'>
#             if not self.coll:
#                 print 'don\'t assign collection'
#             else:
#                 result = self.coll.find(query)
#         except NameError:
#             print 'some fields name are wrong in ', query
#             exit(0)
#         return result
#
#     def insert(self, data):
#         if type(data) is not dict:
#             print 'the type of insert data isn\'t dict'
#             exit(0)
#         # insert会返回新插入数据的_id
#         self.coll.insert(data)
#
#     def remove(self, data):
#         if type(data) is not dict:
#             print 'the type of remove data isn\'t dict'
#             exit(0)
#         # remove无返回值
#         self.coll.remove(data)
#
#     def update(self, data, setdata):
#         if type(data) is not dict or type(setdata) is not dict:
#             print 'the type of update and data isn\'t dict'
#             exit(0)
#         # update无返回值
#         self.coll.update(data,{'$set':setdata})


class MySQLConnect(object):

    @classmethod
    def createConn(cls, mysql_uri):
        conn = MySQLdb.connect(
            host=mysql_uri['host'],
            port=mysql_uri['port'],
            db=mysql_uri['db'],
            user=mysql_uri['user'],
            passwd=mysql_uri['passwd'],
            charset=mysql_uri['charset'],
            use_unicode=True
        )
        return conn

    @classmethod
    def createDbpool(cls, mysql_uri):
        dbpool = adbapi.ConnectionPool(
            'MySQLdb',
            host=mysql_uri['host'],
            port=mysql_uri['port'],
            db=mysql_uri['db'],
            user=mysql_uri['user'],
            passwd=mysql_uri['passwd'],
            # cursorclass=MySQLdb.cursors.DictCursor,
            charset=mysql_uri['charset'],
            use_unicode=True
        )
        return dbpool


class PostgreSQLConnect(object):

    @classmethod
    def createConn(cls, postgresql_uri):
        try:
            conn = psycopg2.connect(
                host=postgresql_uri['host'],
                port=postgresql_uri['port'],
                database=postgresql_uri['database'],
                user=postgresql_uri['user'],
                password=postgresql_uri['password'],
            )
            return conn
        except psycopg2.Error, e:
            print "Postgres Error %s" % e

    @classmethod
    def createDbpool(cls, postgresql_uri):
        try:
            dbpool = adbapi.ConnectionPool(
                'psycopg2',
                host=postgresql_uri['host'],
                port=postgresql_uri['port'],
                database=postgresql_uri['database'],
                user=postgresql_uri['user'],
                password=postgresql_uri['password'],
            )
            return dbpool
        except psycopg2.Error, e:
            print "Mysql Error %d: %s" % (e.args[0], e.args[1])

    @classmethod
    def executeSqli(cls, sqli, postgesql_uri):
        conn = cls.createConn(postgesql_uri)
        try:
            conn.execute(sqli)
            conn.commit()
            conn.close()
        except psycopg2.Error, e:
            print "Mysql Error %d: %s" % (e.args[0], e.args[1])


    # def getCol(self, ):
